package com.silverhaze.aurora.launcher.util;

import javax.swing.*;
import java.awt.*;
import java.util.Arrays;

/**
 * User: Babanin
 * Date: 07.04.11
 * Time: 23:28
 */
public class UIHelper {
    public static Box createHorizontalBox(Object... args) {
        Box hBox = Box.createHorizontalBox();

        addToHorizontalBox(hBox, args);

        return hBox;
    }

    public static Box hBox(Object... args) {
        return createHorizontalBox(args);
    }

    public static void addToHorizontalBox(Box hBox, Object... args) {
        for (Object o : args) {
            if (o instanceof Integer) {
                hBox.add(Box.createHorizontalStrut(Integer.parseInt(o.toString())));
            } else if (o instanceof String) {
                hBox.add(new JLabel(processString(o.toString())));
            } else {
                hBox.add((Component) o);
            }
        }
    }

    public static Box createVerticalBox(Object... args) {
        Box vBox = Box.createVerticalBox();

        addToVerticalBox(vBox, args);

        return vBox;
    }

    public static Box vBox(Object... args) {
        return createVerticalBox(args);
    }

    public static void addToVerticalBox(Box vBox, Object... args) {
        for (Object o : args) {
            if (o instanceof Integer) {
                vBox.add(Box.createVerticalStrut(Integer.parseInt(o.toString())));
            } else if (o instanceof String) {
                vBox.add(new JLabel(processString(o.toString())));
            } else {
                vBox.add((Component) o);
            }
        }
    }

    public static JPanel packInPanel(JComponent component) {
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(component, BorderLayout.CENTER);
        return panel;
    }


    public static JPanel packInPanel(JComponent component, Object constraints) {
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(component, constraints);
        return panel;
    }

    public static JPanel packInTitledPanel(JComponent component, Object constraints, String title) {
        JPanel result = packInPanel(component, constraints);
        result.setBorder(BorderFactory.createTitledBorder(title));
        return result;
    }

    public static JPanel packInLinedPanel(JComponent component, Object constraints) {
        JPanel result = packInPanel(component, constraints);
        result.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        return result;
    }

    public static JPanel packInShiftedPanel(JComponent component, int top, int bottom, int right, int left) {
        JPanel panel = packInPanel(component);
        panel.setBorder(BorderFactory.createEmptyBorder(top, left, bottom, right));
        return panel;
    }

    public static Frame findMainFrame(Class mainFrameClazz) {
        Frame[] frames = Frame.getFrames();
        for (Frame frame : frames) {
            if (mainFrameClazz.isAssignableFrom(frame.getClass())) {
                return frame;
            }
        }

        return null;
    }

    public static void addToPanel(JPanel panel, Object... components) {
        for (Object component : components) {
            if (component instanceof String) {
                panel.add(new JLabel(processString(component.toString())));
            } else {
                panel.add((JComponent) component);
            }
        }
    }

    public static JPanel createBorderLayoutPanel(JComponent[] components, Object[] constraints) {
        if (components.length != constraints.length) {
            throw new IllegalArgumentException("components.length != constraints.length");
        }

        JPanel result = new JPanel(new BorderLayout());

        for (int i = 0; i < components.length; i++) {
            result.add(components[i], constraints[i]);
        }

        return result;
    }

    static String processString(String input) {
        if (input.indexOf('*') != -1) {
            return "<html>" + input.replaceAll("\\*", "<font color=red><b>*</b></font>") + "</html>";
        }

        return input;
    }

    public static ButtonGroup createButtonGroup(AbstractButton... buttonz) {
        ButtonGroup result = new ButtonGroup();

        for (AbstractButton b : buttonz) {
            result.add(b);
        }

        return result;
    }

    public static JFrame packInFrame(String title, JComponent component, Object constraint) {
        JFrame frame = new JFrame(title);

        frame.setLayout(new BorderLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(component, constraint);
        frame.pack();
        frame.setLocationRelativeTo(null);

        return frame;
    }

    public static void fixSize(Component component) {
        Dimension size = component.getPreferredSize();
        size.width = component.getMaximumSize().width;
        component.setMaximumSize(size);
    }

    public static void fixSize(Component component, Dimension size) {
        component.setMinimumSize(size);
        component.setMaximumSize(size);
        component.setPreferredSize(size);
    }

    public static Dimension makeSameSize(JComponent... components) {
        return makeSameSize(Arrays.asList(components));
    }

    public static Dimension makeSameSize(java.util.List<JComponent> components) {
        int[] sizes = new int[components.size()];
        for (int i = 0; i < sizes.length; i++) {
            sizes[i] = components.get(i).getPreferredSize().width;
        }
        int maxSizePos = maximumElementPosition(sizes);
        Dimension maxSize = components.get(maxSizePos).getPreferredSize();

        for (JComponent component : components) {
            fixSize(component, maxSize);
        }
        return maxSize;
    }

    private static int maximumElementPosition(int[] array) {
        int maxPos = 0;
        for (int i = 1; i < array.length; i++) {
            if (array[i] > array[maxPos]) maxPos = i;
        }
        return maxPos;
    }

    public static void setGroupAligmentX(JComponent[] cs, float aligment) {
        for (JComponent c : cs) {
            c.setAlignmentX(aligment);
        }
    }

    public static void setGroupAligmentY(JComponent[] cs, float aligment) {
        for (JComponent c : cs) {
            c.setAlignmentY(aligment);
        }
    }

    public static int getComponentIndex(JComponent parent, Component component) {
        for (int i = parent.getComponents().length; --i >= 0;) {
            if (parent.getComponents()[i] == component) {
                return i;
            }
        }
        return -1;
    }
}
